//
//  YMUserAccountViewModel.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/22.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

class YMUserAccountViewModel: NSObject {
    
    //归档路径
    let filePath = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    //单利viewmodel
    static let sharedTools:YMUserAccountViewModel = YMUserAccountViewModel()
    //用户模型
    var userAccount:YMUserAccount?
    
    var accessToken:String?{
        // 代表用户有accessToken
//        return userAccount?.access_token
        if userAccount?.access_token != nil {
            // 看是否过期
            // 当前保存的日期与当前日期对比 如果为降序 为未过去
//             if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
            }else {
                // 代表已经过期
                return nil
            }
        }else {
            // 代表没有acceToken
            /*
             - 代表用户没有登录 或者没有保存数据
             */
            return nil
        }
    }
 //========================
    
    //通过token判断是否登录
    
    var isLogin:Bool {
        
        return accessToken != nil
    }
    
//==========================
   override init() {
        super.init()
        userAccount = getUserAccount()
    
    //发送通知回main
    
    
    
    }
    

    
    
}
    //        MARK: - 归档解档========
extension YMUserAccountViewModel{
   //归档=======
    func saveUserAccount(userAccount:YMUserAccount){
        
    self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: filePath)
        
    }
    //解档======
    func getUserAccount() ->YMUserAccount?{
        
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? YMUserAccount
        
        return result
    }
}
//        MARK: - 请求数据============
extension YMUserAccountViewModel{
    //请求account
    func loadUserAccount(code:String , isSuccess:(isSucc:Bool) ->()){
        
      
        YMNetworkTools.sharedTools.oauthLoadAccount(code, success: { (response) in
            
            //判断response是否为nil
            
            guard let res = response as? [String:AnyObject] else{
                isSuccess(isSucc: false)
                return
            }
            //转模型
            let userAccount = YMUserAccount(dic: res)
            self.loadUserInfo(userAccount, isSuccess: isSuccess)
            
            }) { (error) in
              isSuccess(isSucc: false)
        }
        
        
    }
    
    
   //请求用户信息======
    func loadUserInfo(userAccount:YMUserAccount ,isSuccess:(isSucc:Bool) ->()) {
        
        YMNetworkTools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) in
            
            guard let res = response as? [String :AnyObject] else{
                isSuccess(isSucc: false)
                return
            }
            
            //获取的到信息归档
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            //=== 归档 ====
            self.saveUserAccount(userAccount)
            isSuccess(isSucc: true)
            }) { (error) in
               isSuccess(isSucc: false)
        }
    }
    
    
}





















